import pandas as pd
import jieba
import matplotlib.pyplot as plt
from tkinter import _flatten
from wordcloud import WordCloud

# 数据的读取
data = pd.read_csv("./data/content.csv")

# 分词
jieba.load_userdict("./data/duoguan.txt")
jieba.add_word('女排牛逼')

# 中文分词
cut = data['content'].apply(jieba.lcut)

# 读取停用词表
with open("./data/stoplist.txt", 'r', encoding='utf-8') as f:
    stop = f.read().split()
stop = stop + [' ', '\n', '\t']
after = cut.apply(lambda s: [w for w in s if w not in stop])

# 统计词频
num = pd.Series(_flatten(list(after))).value_counts()
pic = plt.imread("./data/abc.jpg")

# 词云参数
wc = WordCloud(mask=pic, background_color='white', font_path='C:\Windows\Fonts\STCAIYUN.TTF')
wc.fit_words(num)

# 展示词云
plt.figure(dpi=300)
plt.imshow(wc)
plt.axis('off')
plt.show()
